﻿using Apewer.Network;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Apewer.Web
{

    /// <summary></summary>
    public sealed class MiniProvider : ApiProvider<MiniContext>
    {

        MiniConnection connection;
        MiniContext context;
        MiniRequest request;
        MiniResponse response;

        /// <summary>HttpContext</summary>
        public override MiniContext Context { get => context; }

        /// <summary>创建服务程序实例。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        public MiniProvider(MiniContext context)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            this.context = context;
            connection = context.Connection;
            request = context.Request;
            response = context.Response;
        }

        /// <summary>发送完毕后执行。</summary>
        public override void Sent() => connection.Close();

        /// <summary>结束本次请求和响应。</summary>
        public override void End() => connection.Close(true);

        /// <summary>释放非托管资源。</summary>
        public override void Dispose() => connection.Close(true);

        #region request

        /// <summary></summary>
        public override string GetClientIP() => connection.RemoteEndPoint.Address.ToString();

        /// <summary></summary>
        public override long GetContentLength() => request.ContentLength;

        /// <summary></summary>
        public override string GetContentType() => request.Headers.GetValue("Content-Type");

        /// <summary></summary>
        public override HttpHeaders GetHeaders() => request.Headers;

        /// <summary></summary>
        public override HttpMethod GetMethod() => NetworkUtility.ParseHttpMethod(request.Method);

        /// <summary></summary>
        public override string GetReferrer() => request.Headers.GetValue("Referrer");

        /// <summary></summary>
        public override Uri GetUrl() => request.Url;

        /// <summary></summary>
        public override Stream RequestBody() => request.Body;

        #endregion

        #region response

        /// <summary></summary>
        public override void SetCache(int seconds)
        {
            if (seconds > 0)
            {
                SetHeader("Cache-Control", $"public, max-age={seconds}, s-maxage={seconds}");
                return;
            }

            SetHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            SetHeader("Pragma", "no-cache");
        }

        /// <summary></summary>
        public override void SetContentLength(long value) => response.ContentLength = value;

        /// <summary></summary>
        public override void SetContentType(string value) => response.ContentType = value;

        /// <summary></summary>
        public override string SetHeader(string name, string value) => response.Headers[name] = value;

        /// <summary></summary>
        public override void SetRedirect(string location)
        {
            if (string.IsNullOrEmpty(location)) location = "/";
            response.Status = 302;
            response.Location = location;
            response.Close();
        }

        /// <summary></summary>
        public override void SetStatus(int status, int subStatus = 0) => response.Status = status;

        /// <summary></summary>
        public override Stream ResponseBody() => response.Body;

        #endregion

    }

}
